package androidx.gdata.blogger.xml;

import androidx.gdata.XMLSerializer;
import androidx.gdata.xml.LinkUrlSerializer;
import androidx.gdata.xml.GeneratorSerializer;
import androidx.gdata.xml.OpenSearchSerializer;
import androidx.gdata.xml.CategorySerializer;
import androidx.gdata.xml.AuthorSerializer;
import androidx.gdata.LinkUrl;
import androidx.gdata.Category;
import androidx.gdata.blogger.BlogCollection;
import androidx.gdata.blogger.Blog;

import java.io.PrintWriter;
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @author jennings
 *         Date: Nov 12, 2008
 */
public class BlogCollectionSerializer implements XMLSerializer<BlogCollection> {
  public static final String BLOGGER_FEED_PROLOG = "<feed xmlns='http://www.w3.org/2005/Atom'\n" +
      "      xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'>";

  private LinkUrlSerializer linkSerializer;
  private AuthorSerializer authorSerializer;
  private GeneratorSerializer generatorSerializer;
  private OpenSearchSerializer openSearchSerializer;
  private BlogSerializer entrySerializer;
  private CategorySerializer categorySerializer;

  public BlogCollectionSerializer() {
    linkSerializer = new LinkUrlSerializer();
    authorSerializer = new AuthorSerializer();
    generatorSerializer = new GeneratorSerializer();
    openSearchSerializer = new OpenSearchSerializer();
    entrySerializer = new BlogSerializer();
    categorySerializer = new CategorySerializer();
  }

  public void serialize(PrintWriter out, BlogCollection bloggerFeed) {
    out.println(XML_PROLOG);
    out.println(STYLESHEET);
    out.println(BLOGGER_FEED_PROLOG);
    out.println("  <id>" + bloggerFeed.getId() + "</id>");
    out.println("  <updated>" + bloggerFeed.getUpdated() + "</updated>");
    Category category = bloggerFeed.getCategory();
    if (category != null) {
      categorySerializer.serialize(out, category);
    }
    out.println("  <title type='text'>" + bloggerFeed.getTitle() + "</title>");
    for (LinkUrl link : bloggerFeed.getLinks()) {
      linkSerializer.serialize(out, link);
    }
    authorSerializer.serialize(out, bloggerFeed.getAuthor());
    generatorSerializer.serialize(out, bloggerFeed.getGenerator());
    openSearchSerializer.serialize(out, bloggerFeed.getOpenSearch());
    if (bloggerFeed.getBlogs() != null) {
      for (Blog entry : bloggerFeed.getBlogs()) {
        entrySerializer.serialize(out, entry);
      }
    }
    out.println(FEED_EPILOG);
  }
}